using System;
using System.Runtime.InteropServices;
using System.Security;

namespace ITProf.QuickMailer
{
    internal static class SecureHelper
    {
        public static SecureString ConvertToSecureString(string value)
        {
            var result = new SecureString();
            foreach (char c in value)
            {
                result.AppendChar(c);
            }
            return result;
        }

        public static string ConvertToString(SecureString value)
        {
            IntPtr pointer = Marshal.SecureStringToBSTR(value);
            try
            {
                return Marshal.PtrToStringAuto(pointer);
            }
            finally
            {
                Marshal.ZeroFreeBSTR(pointer);
            }
        }
    }
}